package androtv.utils;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;


import org.apache.http.*;
import org.apache.http.client.*;
import org.apache.http.client.methods.*;
import org.apache.http.impl.client.*;


import android.graphics.Bitmap;
import android.graphics.BitmapFactory;


public class SiteClientUtil {
	
	private SiteClientUtil(){}
	
	public static String getSiteUrl(){
		return "http://json.protv.md/featured?ctr=_" + new Date().getTime();
	}
	
	public static String getNews(){
		 HttpClient httpclient = new DefaultHttpClient();
		 //TODO: format url wit current date
		 String url = getSiteUrl();
		 HttpGet httpget = new HttpGet(url);
		 HttpResponse response;
		 String result = null;

		 try {
	            response = httpclient.execute(httpget);

	            // Get hold of the response entity
	            HttpEntity entity = response.getEntity();
	            // If the response does not enclose an entity, there is no need
	            // to worry about connection release

	            if (entity != null) {
	                // A Simple Response Read
	                InputStream instream = entity.getContent();
	                result = convertStreamToString(instream);

	                // Closing the input stream will trigger connection release
	                instream.close();
	            }
	        } catch (ClientProtocolException e) {
	            // TODO Auto-generated catch block
	            e.printStackTrace();
	        } catch (IOException e) {
	            // TODO Auto-generated catch block
	            e.printStackTrace();
	        }
	        return 	result;

	}
	 
	public static Bitmap getImage(String  url) throws MalformedURLException, IOException {
		  
	    Bitmap bitmap = null;
	    InputStream in = null;       
	       try {
	           in = OpenHttpConnection(url);
	           bitmap = BitmapFactory.decodeStream(in, null, new BitmapFactory.Options());
	           in.close();
	       } catch (IOException e1) {
	       }
	       return bitmap;               
	   }
    
	public static String getVideoUrl(String url) throws IOException{
		InputStream stream = OpenHttpConnection(url);
		String html = convertStreamToString(stream);
		int startIndex = html.indexOf("$('#video').flash(");
		
		if(startIndex==-1){
			return null;
		}
		
		startIndex = html.indexOf("file:", startIndex);
		int endIndex = html.indexOf(",", startIndex);
		
		if(endIndex== -1 ){
			return null;
		}
		
		return html.substring(startIndex + 5, endIndex);
	}
	
	private static InputStream OpenHttpConnection(String url) throws IOException{
		 InputStream inputStream = null;
		 URL _url = new URL(url);
		 URLConnection conn = _url.openConnection();
	
		 try{
			  HttpURLConnection httpConn = (HttpURLConnection)conn;
			  httpConn.setRequestMethod("GET");
			  httpConn.connect();
		
			  if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
			   inputStream = httpConn.getInputStream();
			  }
		 }
		 catch (Exception e)
		 {
			 // TODO Auto-generated catch block
	         e.printStackTrace();
		 }
		 return inputStream;
	}
	
    private static String convertStreamToString(InputStream is) {
	        /*
	        * To convert the InputStream to String we use the BufferedReader.readLine()
	        * method. We iterate until the BufferedReader return null which means
	        * there's no more data to read. Each line will appended to a StringBuilder
	        * and returned as String.
	        */
	        BufferedReader reader = new BufferedReader(new InputStreamReader(is), 8192);
	        StringBuilder sb = new StringBuilder();

	        String line = null;
	        try {
	            while ((line = reader.readLine()) != null) {
	                sb.append(line + "\n");
	            }
	        } catch (IOException e) {
	            e.printStackTrace();
	        } finally {
	            try {
	                is.close();
	            } catch (IOException e) {
	                e.printStackTrace();
	            }
	        }

	        return sb.toString();
	    }
    
}
	

